eureka 服务于发现 (集群模式)
1......pom.xml 父类依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wsc</groupId> 8 <artifactId>spring-cloud-parent</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <modules> 11 <!-- 封装的实体类 product--> 12 <module>../../common</module> 13 <!-- 提供者 端口号:8001--> 14 <module>../../provider</module> 15 <!-- 消费者 端口号:81--> 16 <module>../../consumer</module> 17 <!-- 注册中心 端口号:6001--> 18 <module>../../eureka-6001</module> 19 <!-- 注册中心 端口号:6002--> 20 <module>../../eureka-6002</module> 21 <!-- 提供者 端口号:8002--> 22 <module>../../common-common</module> 23 <!-- 消费者 端口号:80--> 24 <module>../../comsumer-consumer</module> 25 </modules> 26 <!--手动指定pom--> 27 <packaging>pom</packaging> 28 <!--springboot版本 2.0.7--> 29 <parent> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-parent</artifactId> 32 <version>2.0.7.RELEASE</version> 33 </parent> 34 <properties> 35 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 36 <maven.compiler.source>1.8</maven.compiler.source> 37 <maven.compiler.target>1.8</maven.compiler.target> 38 <junit.version>4.12</junit.version> 39 <!--spring Cloud 最新的稳定版 Finchley SR2--> 40 <spring.cloud.version>Finchley.SR2</spring.cloud.version> 41 </properties> 42 <!--依赖声明--> 43 <dependencyManagement> 44 <dependencies> 45 <dependency> 46 <groupId>org.springframework.cloud</groupId> 47 <artifactId>spring-cloud-dependencies</artifactId> 48 <version>${spring.cloud.version}</version> 49 <!-- 不可或缺--> 50 <scope>import</scope> 51 <type>pom</type> 52 </dependency> 53 <dependency> 54 <groupId>org.mybatis.spring.boot</groupId> 55 <artifactId>mybatis-spring-boot-starter</artifactId> 56 <version>1.3.2</version> 57 </dependency> 58 <!--数据源--> 59 <dependency> 60 <groupId>com.alibaba</groupId> 61 <artifactId>druid</artifactId> 62 <version>1.1.12</version> 63 </dependency> 64 <!-- 数据库 8.0.13--> 65 <dependency> 66 <groupId>mysql</groupId> 67 <artifactId>mysql-connector-java</artifactId> 68 <version>8.0.13</version> 69 </dependency> 70 <!--测试单元--> 71 <dependency> 72 <groupId>junit</groupId> 73 <artifactId>junit</artifactId> 74 <version>${junit.version}</version> 75 <scope>test</scope> 76 </dependency> 77 </dependencies> 78 </dependencyManagement> 79 </project>
2......common 实体类
1 package com.wsc.core.pojo; 2 3 /** 4 * @version 1.0 5 * @ClassName Product 6 * @Description TODO 7 * @Author WSC 8 * @Date 2019/8/26 14:53 9 **/ 10 public class Product { 11 private Long pid; 12 private String productName; 13 private String dbSource; 14 15 public Product(String productName) { 16 this.productName = productName; 17 } 18 19 public Product(Long pid, String productName, String dbSource) { 20 this.pid = pid; 21 this.productName = productName; 22 this.dbSource = dbSource; 23 } 24 public Product() { 25 } 26 27 public Long getPid() { 28 return pid; 29 } 30 31 public void setPid(Long pid) { 32 this.pid = pid; 33 } 34 35 public String getProductName() { 36 return productName; 37 } 38 39 public void setProductName(String productName) { 40 this.productName = productName; 41 } 42 43 public String getDbSource() { 44 return dbSource; 45 } 46 47 public void setDbSource(String dbSource) { 48 this.dbSource = dbSource; 49 } 50 }
3......eureka-6001 eureka注册中心 端口号:6001
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>eureka-6001</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <!--导入Eureka Server服务端依赖--> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 26 </dependency> 27 </dependencies> 28 </project>
启动类
1 package com.wsc.core.eureka; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 /** 8 * @version 1.0 9 * @ClassName Start_6001 10 * @Description TODO 11 * @Author WSC 12 * @Date 2019/8/28 14:11 13 **/ 14 @EnableEurekaServer //声明注册中心 15 @SpringBootApplication 16 public class Start_6001 { 17 public static void main(String[] args) { 18 SpringApplication.run(Start_6001.class,args); 19 } 20 }
yml 配置文件
1 server: 2 port: 6001 3 eureka: 4 instance: 5 hostname: eureka6001.com 6 server: 7 enable-self-preservation: false # 禁用自我保护模式 8 client: 9 register-with-eureka: false #服务注册开关 10 fetch-registry: false #服务发现开关 11 service-url: 12 defaultZone: http://eureka6002.com:6002/eureka/ # 1 显示主机名 单机是配置自己 如果不配置默认端口是8761 13 # http://${eureka.instance.hostname}:${server.port}/eureka # 不显示主机名
4......eureka-6002 eureka注册中心 端口号:6002
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>eureka-6002</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.cloud</groupId> 24 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 25 </dependency> 26 27 </dependencies> 28 </project>
启动类
1 package com.wsc.core.eureka; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 /** 8 * @version 1.0 9 * @ClassName Start_6002 10 * @Description TODO 11 * @Author WSC 12 * @Date 2019/8/28 16:54 13 **/ 14 @EnableEurekaServer //声明注册中心 15 @SpringBootApplication 16 public class Start_6002 { 17 public static void main(String[] args) { 18 SpringApplication.run(Start_6002.class,args); 19 } 20 }
yml配置
1 server: 2 port: 6002 3 eureka: 4 instance: 5 hostname: eureka6002.com 6 server: 7 enable-self-preservation: false # 禁用自我保护模式 8 client: 9 register-with-eureka: false #服务注册开关 10 fetch-registry: false #服务发现开关 11 service-url: 12 defaultZone: http://eureka6001.com:6001/eureka/ # 1 显示主机名 单机是配置自己 如果不配置默认端口是8761 13 # http://${eureka.instance.hostname}:${server.port}/eureka # 不显示主机名
5.... C:// windows / system32 / drivers / etc / hosts (末尾添加)
添加属性配置关联:
127.0.0.1 eureka6002.com
127.0.0.1 eureka6002.com
需要刷新:1 :重启电脑 2 : dos 窗口 执行代码 :1..... ipconfig / displaydns 2..... ipconfig / flushdns
测试网址输入:
http://eureka6001.com:6001/ http://eureka6002.com:6002/
6..... provider 提供者 端口号:8001
resources / mybatis / mapper / ProductMapper.xml 配置 映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.wsc.core.mapper.ProductMapper"> 5 <select id="findAll" resultType="Product"> 6 select * from product; 7 </select> 8 9 <select id="findById" resultType="Product" parameterType="Long"> 10 select * from product where pid=#{pid}; 11 </select> 12 13 <insert id="add" parameterType="Product" > 14 insert into product values (null,#{product_name},#{db_source}); 15 </insert> 16 </mapper>
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>provider</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>com.wsc</groupId> 24 <artifactId>common</artifactId> 25 <version>${project.version}</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>mysql</groupId> 33 <artifactId>mysql-connector-java</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.mybatis.spring.boot</groupId> 37 <artifactId>mybatis-spring-boot-starter</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>junit</groupId> 41 <artifactId>junit</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>com.alibaba</groupId> 45 <artifactId>druid</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 </dependency> 51 <!--服务提供者注册进服务中心--> 52 <dependency> 53 <groupId>org.springframework.cloud</groupId> 54 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 55 </dependency> 56 </dependencies> 57 </project>
resources / mybatis / mybatis.cfg.xml 配置 开启驼峰命名
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <settings> 7 <!--开启驼峰命名--> 8 <setting name="mapUnderscoreToCamelCase" value="true"/> 9 </settings> 10 </configuration>
resources / application.yml 数据库 扫描文件 注册客户端
1 server: 2 port: 8001 3 mybatis: 4 config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 配置文件路径 5 type-aliases-package: com.wsc.core.pojo # entity别名类所在包 6 mapper-locations: mybatis/mapper/*.xml # mapper映射文件 7 spring: 8 application: 9 name: microserver-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 10 datasource: 11 type: com.alibaba.druid.pool.DruidDataSource 12 driver-class-name: com.mysql.cj.jdbc.Driver 13 url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8 14 password: wsc 15 username: root 16 dbcp2: 17 min-idle: 5 # 数据库连接池的最小维持连接数 18 initial-size: 5 # 初始化连接数 19 max-total: 5 # 最大连接数 20 max-wait-millis: 150 # 等待连接获取的最大超时时间 21 22 eureka: 23 client: 24 register-with-eureka: true #服务注册开关 25 fetch-registry: true #服务发现开关 26 service-url: 27 defaultZone: http://eureka6001.com:6001/eureka/, http://eureka6002.com:6002/eureka/ # http://localhost:6001/eureka # 1 显示主机名 28 instance: 29 instanceId: ${spring.application.name}:${server.port} # 2 指定实例ID 不显示主机名 30 preferipAddress: true
service / ProductService
1 package com.wsc.core.service; 2 3 import com.wsc.core.pojo.Product; 4 5 import java.util.List; 6 7 public interface ProductService { 8 public List<Product> findAll(); 9 10 public Product findById(Long id); 11 12 public Boolean add(Product product); 13 }
service /impl /
1 package com.wsc.core.service.impl; 2 3 import com.wsc.core.mapper.ProductMapper; 4 import com.wsc.core.pojo.Product; 5 import com.wsc.core.service.ProductService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.util.List; 10 11 /** 12 * @version 1.0 13 * @ClassName ProductServiceImpl 14 * @Description TODO 15 * @Author WSC 16 * @Date 2019/8/27 10:53 17 **/ 18 @Service 19 public class ProductServiceImpl implements ProductService { 20 @Autowired 21 private ProductMapper productMapper; 22 @Override 23 public List<Product> findAll() { 24 return productMapper.findAll(); 25 } 26 27 @Override 28 public Product findById(Long id) { 29 return productMapper.findById(id); 30 } 31 32 @Override 33 public Boolean add(Product product) { 34 return productMapper.add(product); 35 } 36 }
mapper /
1 package com.wsc.core.mapper; 2 3 import com.wsc.core.pojo.Product; 4 5 import java.util.List; 6 7 public interface ProductMapper { 8 public List<Product> findAll(); 9 10 public Product findById(Long id); 11 12 public Boolean add(Product product); 13 }
controller /
1 package com.wsc.core.controller; 2 3 import com.wsc.core.pojo.Product; 4 import com.wsc.core.service.ProductService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.*; 7 8 import java.util.List; 9 10 /** 11 * @version 1.0 12 * @ClassName ProductController 13 * @Description TODO 14 * @Author WSC 15 * @Date 2019/8/27 11:06 16 **/ 17 @RestController //返回json数据 18 public class ProductController { 19 @Autowired 20 private ProductService productService; 21 //查询全部 22 @RequestMapping(value ="/product/get/list",method = RequestMethod.GET) 23 public List<Product> getAll(){ 24 return productService.findAll(); 25 } 26 27 //查询id 28 @RequestMapping(value = "/product/get/{pid}",method = RequestMethod.GET) 29 public Product getById(@PathVariable("pid")Long pid){ 30 return productService.findById(pid); 31 } 32 33 //添加 34 @RequestMapping(value ="/product/get/add",method = RequestMethod.POST) 35 public boolean add(@RequestBody Product product){ 36 return productService.add(product); 37 } 38 }
Start_8001
1 package com.wsc.core; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 8 /** 9 * @version 1.0 10 * @ClassName Start 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/8/27 10:54 14 **/ 15 @EnableEurekaClient // 本服务启动后 自动注册进Eureka中心 16 @MapperScan("com.wsc.core.mapper") //mapper扫描包 类 ProductMapper 17 @SpringBootApplication 18 public class Start_8001 { 19 public static void main(String[] args) { 20 SpringApplication.run(Start_8001.class,args); 21 } 22 }
测试网址输入
http://localhost:8001/product/get/list
7.....common-common 提供者 端口号:8002
application.yml 更改数据库名 端口号
1 server: 2 port: 8002 3 mybatis: 4 config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 配置文件路径 5 type-aliases-package: com.wsc.core.pojo # entity别名类所在包 6 mapper-locations: mybatis/mapper/*.xml # mapper映射文件 7 spring: 8 application: 9 name: microserver-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 10 datasource: 11 type: com.alibaba.druid.pool.DruidDataSource 12 driver-class-name: com.mysql.cj.jdbc.Driver 13 url: jdbc:mysql://127.0.0.1:3306/springcloud_db02?serverTimezone=GMT%2B8 14 password: wsc 15 username: root 16 dbcp2: 17 min-idle: 5 # 数据库连接池的最小维持连接数 18 initial-size: 5 # 初始化连接数 19 max-total: 5 # 最大连接数 20 max-wait-millis: 150 # 等待连接获取的最大超时时间 21 22 eureka: 23 client: 24 register-with-eureka: true #服务注册开关 25 fetch-registry: true #服务发现开关 26 service-url: 27 defaultZone: http://eureka6001.com:6001/eureka/, http://eureka6002.com:6002/eureka/ # http://localhost:6001/eureka # 1 显示主机名 28 instance: 29 instanceId: ${spring.application.name}:${server.port} # 2 指定实例ID 不显示主机名 30 preferipAddress: true
其他文件均与 6 号 一样
8.....consumer 消费者 端口号:81 RestTemplate+Ribbon去调用服务者
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>consumer</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>com.wsc</groupId> 24 <artifactId>common</artifactId> 25 <version>${project.version}</version> 26 </dependency> 27 <!--springboot web启动器--> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-web</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.cloud</groupId> 34 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 35 </dependency> 36 </dependencies> 37 </project>
resources / application.yml 消费注册者
1 server: 2 port: 81 3 eureka: 4 client: 5 register-with-eureka: true #服务注册开关 6 fetch-registry: true #服务发现开关 7 service-url: 8 defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/ # http://localhost:6001/eureka # 1 显示主机名 9 # decoder-name: 10 # instance: 11 # instanceId: ${spring.application.name}:${server.port} # 2 指定实例ID 不显示主机名 12 # preferipAddress: true
ConfigBean 开启负载均衡
1 package com.wsc.core.config; 2 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.web.client.RestTemplate; 7 8 /** 9 * @version 1.0 10 * @ClassName ConfigBean 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/8/27 14:32 14 * @LoadBalanced 表示这个RestTemplate开启负载均衡 在调用服务者提供的接口时 可以使用服务名称 15 * * 替代真实的IP 地址 16 **/ 17 @Configuration 18 public class ConfigBean { 19 // 向容器中添加RestTemplate 组件 直接该组件调用EREST接口 20 @LoadBalanced 21 @Bean 22 public RestTemplate getRestTemplate(){ 23 return new RestTemplate(); 24 } 25 }
ConfigBeanController 实现消费地址
1 package com.wsc.core.controller; 2 3 import com.wsc.core.pojo.Product; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.RequestBody; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import org.springframework.web.client.RestTemplate; 10 11 import java.util.List; 12 13 /** 14 * @version 1.0 15 * @ClassName ConfigBeanController 16 * @Description TODO 17 * @Author WSC 18 * @Date 2019/8/27 14:33 19 **/ 20 @RestController 21 public class ConfigBeanController { 22 //provider的访问 端口 23 // private static final String REST_URL_PREFIX="http://localhost:8001"; 24 private static final String REST_URL_PREFIX="http://MICROSERVER-PRODUCT"; 25 @Autowired 26 private RestTemplate restTemplate; 27 @RequestMapping("/consumer/get/add") 28 public boolean add(@RequestBody Product product){ 29 return restTemplate.postForObject(REST_URL_PREFIX+"/product/get/add",product,boolean.class); 30 } 31 32 @RequestMapping("/consumer/get/findAll") 33 public List<Product> getAll(){ 34 return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/list",List.class); 35 } 36 37 @RequestMapping("/consumer/product/get/{pid}") 38 public Product getById(@PathVariable("pid") Long pid){ 39 return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/"+pid,Product.class); 40 } 41 }
Start_800 启动类
1 package com.wsc.core; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 7 /** 8 * @version 1.0 9 * @ClassName Start_80 10 * @Description TODO 11 * @Author WSC 12 * @Date 2019/8/27 14:31 13 **/ 14 @EnableEurekaClient 15 @SpringBootApplication 16 public class Start_800 { 17 public static void main(String[] args) { 18 SpringApplication.run(Start_800.class,args); 19 } 20 }
测试网址输入:
http://localhost:81/consumer/product/get/1
9.....comsumer-consumer 消费者 端口号:80 通过Fegin去调用服务者 ,Fegin(遵循面向接口编程)
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>comsumer-consumer</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>com.wsc</groupId> 24 <artifactId>common</artifactId> 25 <version>1.0-SNAPSHOT</version> 26 </dependency> 27 <!--springboot web启动器--> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-web</artifactId> 31 </dependency> 32 <!--Ribbon 相关的依赖--> 33 <!--spring-cloud-starter-netflix-eureka-client 会自动添加spring-cloud-starter-netflix-ribbon--> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 37 </dependency> 38 <!--feign的依赖--> 39 <dependency> 40 <groupId>org.springframework.cloud</groupId> 41 <artifactId>spring-cloud-starter-openfeign</artifactId> 42 </dependency> 43 </dependencies> 44 </project>
resources / application.yml 消费注册者
1 server: 2 port: 80 3 eureka: 4 client: 5 register-with-eureka: true #服务注册开关 6 fetch-registry: true #服务发现开关 7 service-url: 8 defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/ # http://localhost:6001/eureka # 1 显示主机名 9 # decoder-name: 10 # instance: 11 # instanceId: ${spring.application.name}:${server.port} # 2 指定实例ID 不显示主机名 12 # preferipAddress: true
FeignService service 类 (service 的requestMapping地址 必须与 提供者 (8001 , 8002)的 controller 地址 保持一致 , 否则会报错)
1 package com.wsc.core.service; 2 3 import com.wsc.core.pojo.Product; 4 import org.springframework.cloud.openfeign.FeignClient; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 9 import java.util.List; 10 11 @FeignClient(value = "microserver-product") 12 public interface FeignService { 13 @RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET) 14 Product get(@PathVariable Long id); 15 @RequestMapping(value = "/product/list",method = RequestMethod.GET) 16 List<Product> list(); 17 @RequestMapping(value = "/product/add",method = RequestMethod.POST) 18 boolean add(Product product); 19 }
ConfigBeanController 实现消费地址
1 package com.wsc.core.controller; 2 3 import com.wsc.core.pojo.Product; 4 import com.wsc.core.service.FeignService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import java.util.List; 11 12 /** 13 * @version 1.0 14 * @ClassName ConfigBeanController 15 * @Description TODO 16 * @Author WSC 17 * @Date 2019/8/27 14:33 18 **/ 19 @RestController 20 public class ConfigBeanController { 21 @Autowired 22 private FeignService feignService; 23 @RequestMapping(value ="/product/add") 24 public boolean add(Product product){ 25 return feignService.add(product); 26 } 27 28 @RequestMapping(value = "/consumer/{id}") 29 public Product get(@PathVariable("id") Long id) { 30 return feignService.get(id); 31 } 32 @RequestMapping(value = "/consumer/product/list") 33 public List<Product> list() { 34 return feignService.list(); 35 } 36 }
Start_800_feign 启动类
1 package com.wsc.core; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 import org.springframework.cloud.openfeign.EnableFeignClients; 7 8 /** 9 * @version 1.0 10 * @ClassName Start_80 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/8/27 14:31 14 **/ 15 @EnableFeignClients(basePackages ="com.wsc.core") //feign 通过接口+注解 获得服务的调用 16 @EnableEurekaClient //自动注册eureka 17 @SpringBootApplication 18 public class Start_800_feign { 19 public static void main(String[] args) { 20 SpringApplication.run(Start_800_feign.class,args); 21 } 22 }
测试地址:
http://localhost/consumer/1