您的位置:首页 > 健康 > 美食 > 网站建设方案书的内容_宁波seo推广经验_宁波建站模板系统_武汉网络推广自然排名

网站建设方案书的内容_宁波seo推广经验_宁波建站模板系统_武汉网络推广自然排名

2024/12/25 14:48:03 来源:https://blog.csdn.net/wang_san_sui_/article/details/142626416  浏览:    关键词:网站建设方案书的内容_宁波seo推广经验_宁波建站模板系统_武汉网络推广自然排名
网站建设方案书的内容_宁波seo推广经验_宁波建站模板系统_武汉网络推广自然排名

1、简介

dubbo框架是做微服务通信的,是由阿里巴巴开发,后捐赠给阿帕奇基金会。

2、与OpenFeign的区别

dubbo是采用RPC协议实现微服务通信,OpenFeign是采用Http请求的方式实现的。

OpenFeign 最简单的,就是Spring公司开发的,整合SpringBoot的时候,代码简洁。

3、应用

要创建三个工程,api工程,消费者工程(consumer)和提供者工程(provider),api工程是用于另外两个工程引用的。

消费者工程和提供者工程 是要有共同的引用的,比如说:在传递请求的时候,要用user对象,那这个user对象就可以定义在api工程里,这样两个工程就都可以用了。还有Service层接口,也是要放到api工程里的。

0、启动nacos和redis

1、准备工程

创建三个项目

2、编写api工程

【1.添加依赖】

 <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version><jwt.version>0.7.0</jwt.version><fastjson.version>1.2.60</fastjson.version><spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version><spring-cloud.version>Finchley.SR2</spring-cloud.version></properties><dependencies>
​<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version><scope>compile</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version><scope>compile</scope></dependency>
​</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
​<!--2.添加当前项目所依赖的插件: --><build><plugins><!--1.添加tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><path>/</path></configuration></plugin><!--2.添加Spring Boot 的打包插件 --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.1.10.RELEASE</version><configuration><fork>true</fork></configuration></plugin></plugins></build>

【2.添加实体类Score】

package com.jr.entry;
​
import lombok.Data;
import java.io.Serializable;
​
@Data
public class Score implements Serializable {private String name;private Double score;
}

注意:实体类Score一定要实现Serializable接口,否则会报错

【3.添加ScoreService接口】

package com.jr.service;
​
import com.jr.entry.Score;
import java.util.List;
​
public interface ScoreService {
​public List<Score> getinfo();
​
}

【4.添加工具类Result】

package com.jr.util;
​
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
​
@Data
public class Result {
​private Integer code;private String message;private Map<String, Object> map = new HashMap<>();
​private Result() {}
​public static Result ok() {Result r = new Result();r.setCode(ResultCode.SUCCESS.getCode());r.setMessage(ResultCode.SUCCESS.getMessage());return r;}
​public static Result error() {Result r = new Result();r.setCode(ResultCode.ERROR.getCode());r.setMessage(ResultCode.ERROR.getMessage());return r;}
​public Result put(String key, Object value) {map.put(key, value);return this;}
​public Object get(String key) {return map.get(key);}
}

【5.添加枚举ResultCode】

package com.jr.util;
​
public enum ResultCode {SUCCESS(0, "请求成功"),ERROR(1, "请求失败"),;
​private int code;private String message;
​ResultCode(int code, String message) {this.code = code;this.message = message;}
​public int getCode() {return code;}
​public String getMessage() {return message;}
}

3、准备Consumer工程

【1.添加依赖】

    
 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version></parent>
​<properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version><jwt.version>0.7.0</jwt.version><fastjson.version>1.2.60</fastjson.version><spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version><spring-cloud.version>Finchley.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>compile</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--json--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!--引入api工程依赖 --><dependency><groupId>com.jr</groupId><artifactId>dubboapi</artifactId><version>1.0-SNAPSHOT</version></dependency><!--引入dubbo依赖 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency>
​</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
​<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build>

【2.创建SessionConfig配置类】

package com.jr.config;
​
@Configuration
public class SessionConfig {
​@Beanpublic CookieSerializer cookieSerializer() {DefaultCookieSerializer serializer = new DefaultCookieSerializer();serializer.setCookieName("JSESSIONID");serializer.setCookiePath("/");serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");return serializer;}
​@Beanpublic RedisSerializer<Object> redisSerializer() {return new GenericJackson2JsonRedisSerializer();}
}

【3.创建控制器UserController】

package com.jr.controller;
​
@RestController
@RequestMapping("/user")
public class UserController {
​@Autowiredprivate UserService userService;
​@GetMappingpublic Result getinfo(){User user = userService.getinfo();return Result.ok().put("data",user);}
}

【4.创建实体类User】

package com.jr.entry;
​
@Component
@Data
public class User {
​private String id;private String name;private String password;private List<Score> scoreList;
}

【5.创建UserService接口】

package com.jr.service;
​
public interface UserService {
​public User getinfo();
}

【6.创建UserServiceImpl实现类】

package com.jr.service.impl;
​
@Service
public class UserServiceImpl implements UserService {
​@Referenceprivate ScoreService scoreService;
​@Overridepublic User getinfo() {User user=new User();user.setId("111");user.setName("zhangsan");user.setPassword("zs123");List<Score> list=scoreService.getinfo();user.setScoreList(list);return user;}
}

注意:@Service主键要用dubbo下面的,不能用Spring的注解。

【7.创建启动类】

package com.jr;
​
@SpringBootApplication
@EnableRedisHttpSession
public class SpringBootMain {public static void main(String[] args) {SpringApplication.run(SpringBootMain.class,args);}
}

【8.编写application.properties配置文件】

spring.application.name=dubbocon

【9.编写application.yml配置文件】

server:port: 100
​
spring:session:store-type: redis
​
dubbo:registry:address: nacos://192.168.43.8:8848cloud:subscribed-services: dubbopro   #是p工程的服务名consumer:check: false

【10.编写bootstrap.yml配置文件】

spring:cloud:nacos:discovery:server-addr: 192.168.43.8:8848config:server-addr: 192.168.43.8:8848file-extension: yamlnamespace: d2a5ce42-8836-4dc4-89e0-9f8620b82327shared-configs:- data-id: dz14redissession.yamlgroup: DEFAULT_GROUPinetutils:preferred-networks: 192.168.56
​

4、准备Provider工程

【1.添加依赖】同上

【2.创建SessionConfig配置类】同上

【3.创建ScoreServiceImpl接口实现类】

package com.jr.service.impl;
​
@Service
public class ScoreServiceImpl implements ScoreService {@Overridepublic List<Score> getinfo() {List<Score> list=new ArrayList<>();for(int i=0;i<3;i++){Score score=new Score();score.setName("admin");score.setScore(Math.random()*100);list.add(score);}return list;}
}

【4.创建启动类】同上

【5.编写application.properties配置文件】

spring.application.name=dubbopro

【6.编写application.yml配置文件】

server:port: 200
​
spring:session:store-type: redis
​
dubbo:scan:base-packages: com.jr.service  #指定把哪一个包下面的Servlet交给dubbo去管理。protocol:name: dubboport: -1registry:address: nacos://192.168.43.8:8848cloud:subscribed-services: ""

【7.编写bootstrap.yml配置文件】同上

注意:

启动时,要先启动consumer工程,后启动provider工程,避免出现启动失败的错误。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com