SpringCloud负载均衡服务配置之一《配置中心》
Eureka配置中心
创建一个项目pom.xml文件如下:
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 top.xzhand.junecloud junecloud pom 1.0-SNAPSHOT eureka admin common admin-product web admin-product1 UTF-8 UTF-8 1.8 Finchley.RELEASE org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-netflix-core 2.0.1.RELEASE compile org.springframework.cloud spring-cloud-starter-ribbon 1.3.4.RELEASE org.springframework.cloud spring-cloud-starter-feign 1.4.4.RELEASE org.springframework.cloud spring-cloud-openfeign-core 2.0.1.RELEASE org.springframework.cloud spring-cloud-starter-eureka 1.4.4.RELEASE com.alibaba fastjson 1.2.32 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
创建一个module模块eureka
pom.xml
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> junecloud top.xzhand.junecloud 1.0-SNAPSHOT 4.0.0 eureka
配置启动项
添加@EnableEurekaServer 标识注册中心项目
package top.xzhand.cloud.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
配置文件设置
application.yml
server: port: 8090 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application:
SpringCloud负载均衡服务配置之二《服务提供者》
创建公用module模块
pom.xml
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> junecloud top.xzhand.junecloud 1.0-SNAPSHOT 4.0.0 common org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 2.8.0 io.springfox springfox-swagger-ui 2.8.0 com.mangofactory swagger-springmvc 1.0.2 com.mangofactory swagger-models 1.0.2 com.wordnik swagger-annotations 1.3.11 org.projectlombok lombok 1.16.6 com.ctrip.framework.apollo apollo-client 1.4.0 org.springframework.boot spring-boot-starter-data-redis redis.clients jedis org.apache.commons commons-pool2 2.4.2 org.springframework.boot spring-boot-configuration-processor true org.apache.httpcomponents httpclient 4.5.2 org.apache.httpcomponents httpcore 4.4.4 org.apache.httpcomponents httpmime 4.5.2 commons-net commons-net 3.6
创建服务提供者module
pom.xml
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> junecloud top.xzhand.junecloud 1.0-SNAPSHOT 4.0.0 admin-product 1.0-SNAPSHOT top.xzhand.junecloud common ${junecloud-common.version} com.alibaba druid-spring-boot-starter 1.1.0 mysql mysql-connector-java org.springframework.boot spring-boot-maven-plugin repackage org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 true true mysql mysql-connector-java 5.1.22 src/main/java **/*.yml **/*.properties **/*.xml false src/main/resources **/*.yml **/*.properties **/*.xml false
启动项创建
@EnableDiscoveryClient 连接配置中心注解
package top.xzhand.cloud.admin.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * 负载均衡服务提供者 */ @SpringBootApplication @EnableDiscoveryClient public class ProductServerApplication { public static void main(String[] args) { SpringApplication.run(ProductServerApplication.class, args); } }
数据库配置
package top.xzhand.cloud.admin.product.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class Druidconfig { @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource druidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } }
服务提供者应用创建
package top.xzhand.cloud.admin.product.controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * feign的方式进行服务调 * @param name * @return */ @RequestMapping("/hello") public String index(@RequestParam String name) { return "这是服务提供者,参数:"+name; } /** * 使用rest+ribbon * @param name * @return */ @RequestMapping("/hellorest") public String hellorest(@RequestBody String name) { return "这是服务提供者,参数:"+name; } }
配置文件
application.yml
spring: application: name: spring-cloud-producer datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/june?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false username: root password: root123 type: com.alibaba.druid.pool.DruidDataSource druid: #最大活跃数 maxActive: 20 #初始化数量 initialSize: 1 #最大连接等待超时时间 maxWait: 60000 #打开PSCache,并且指定每个连接PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 #通过connectionProperties属性来打开mergeSql功能;慢SQL记录 #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 1 from dual testWhileIdle: true testOnBorrow: false testOnReturn: false #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,‘wall‘用于防火墙 filters: stat, wall, log4j #mybatis mybatis: # 数据库映射实体类包路径 type-aliases-package: top.xzhand.po # 指定mybatis的xml文件路径 mapper-locations: classpath*:top/xzhand/mapper/impl/*.xml # 表中的字段名与对象的属性名下划线和驼峰转换 configuration: map-underscore-to-camel-case: true # mapper-locations: classpath:/mybatis/*.xml server: port: 8091 eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:8090/eureka/
重复上面操作,创建第二个服务提供者。。。。。。。。
SpringCloud负载均衡服务配置之三《消费者》
创建消费者模块 ribbon调用方式
pom.xml
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> junecloud top.xzhand.junecloud 1.0-SNAPSHOT 4.0.0 web 1.0-SNAPSHOT top.xzhand.junecloud common ${junecloud-common.version}
服务启动项
package top.xzhand.cloud.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * 使用rest+ribbon实现服务调用和服务提供者的负载均衡 */ @SpringBootApplication @EnableDiscoveryClient public class WebServerApplication { public static void main(String[] args) { SpringApplication.run(WebServerApplication.class, args); } }
ribbon负载均衡配置项
package top.xzhand.cloud.web.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; /** * 实现负载均衡 */ @Configuration public class RestTemplateConfig { @Bean @LoadBalanced//追加ribbon负载功能 public RestTemplate restTemplate(){ RestTemplate restTemplate= new RestTemplate(); //解决调用乱码问题 restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); return restTemplate; } }
消费者应用
package top.xzhand.cloud.web.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class HelloController { @Autowired private RestTemplate restTemplate; @PostMapping("/hello") public String hello(String name){ System.out.println(name); String url = "http://spring-cloud-producer/hellorest"; String r = restTemplate.postForObject(url, name,String.class); return r; } }
配置文件
application.yml
spring: application: name: spring-cloud-consumer server: port: 8093 eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:8090/eureka/
创建消费者模块 feign的方式进行服务调
pom.xml
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> junecloud top.xzhand.junecloud 1.0-SNAPSHOT 4.0.0 web 1.0-SNAPSHOT top.xzhand.junecloud common ${junecloud-common.version}
启动项
@EnableFeignClients feign的方式进行服务调必须加上
package top.xzhand.cloud.admin; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 使用feign的方式进行服务调 */ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }
feign的方式配置项
package top.xzhand.cloud.admin.remote; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
消费应用
package top.xzhand.cloud.admin.controller; import com.alibaba.fastjson.JSON; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import top.xzhand.cloud.admin.remote.HelloRemote; @RestController public class HelloController { @Autowired private HelloRemote helloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return helloRemote.hello(name); } }
配置文件
application.yml
spring: application: name: spring-cloud-consumer server: port: 8092 eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:8090/eureka/