项ç®çå¯å¨é¡ºåºä¸º
spring-cloud-eureka æä¾æ³¨åæå¡ï¼èç¹ä¸¤ä¸ªãresumer-eurake-9001ãresumer-eurake-9002ã
å¾®æå¡ï¼æä¾æå¡ï¼ï¼èç¹ä¸¤ä¸ªãresumer-userãresumer-user2forTestã
spring-cloud-feignè°ç¨å¾®æå¡ï¼æå¡æ¶è´¹ç«¯ï¼ï¼ãresumer-feignã
1 spring-cloud-eureka
1.1 resumer-eurake-9001
resumer-eurake-9001é ç½®æ件application.ymlï¼
eureka:
instance:
hostname: eureka9001.com
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://192.168.0.104:9002/eureka/
# å
³é注åä¸å¿çèªæä¿æ¤æºå¶ï¼é²æ¢å·²å
³éçå®ä¾æ æ³ä»æ³¨åä¸å¿åé¤
#eureka.server.enable-self-preservation=false
server:
port: 9001
logging:
config: classpath:logback.xml
å¯å¨ç±»ï¼EurekaApplication9001.java
package com.asiainfo.resumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
//æ¿æ´»eurekaçæå¡ç«¯
@EnableEurekaServer
public class EurekaApplication9001 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication9001.class, args);
}
}
1.2 resumer-eurake-9002
resumer-eurake-9002é ç½®æ件application.ymlï¼
eureka:
instance:
hostname: eureka9002.com
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://192.168.0.104:9001/eureka/
# å
³é注åä¸å¿çèªæä¿æ¤æºå¶ï¼é²æ¢å·²å
³éçå®ä¾æ æ³ä»æ³¨åä¸å¿åé¤
#eureka.server.enable-self-preservation=false
server:
port: 9002
logging:
config: classpath:logback.xml
å¯å¨ç±»ï¼EurekaApplication9002.java åEurekaApplication9001.java ä¸æ ·ã
2 å¾®æå¡ï¼æä¾æå¡ï¼ï¼èç¹ä¸¤ä¸ªãresumer-userãresumer-user2forTestã
2.1 resumer-user é ç½®
resumer-user é ç½®æ件ymlï¼
server:
port: 8002
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatisé
ç½®æ件æå¨è·¯å¾
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapperæ å°æ件
type-aliases-package: com.asiainfo.resumer # å«åç±»æå¨å
spring:
application:
name: resumer-user #å¾®æå¡çåå
datasource:
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱å¨å
type: com.alibaba.druid.pool.DruidDataSource # å½åæ°æ®æºæä½ç±»å
url: jdbc:mysql://localhost:3306/cloudagenttest # æ°æ®åºå称
username: root
password: root
dbcp2:
initial-size: 5 # åå§åè¿æ¥æ°
max-total: 5 # æ大è¿æ¥æ°
max-wait-millis: 200 # çå¾
è¿æ¥è·åçæ大è¶
æ¶æ¶é´
min-idle: 5 # æ°æ®åºè¿æ¥æ± çæå°ç»´æè¿æ¥æ°
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka,http://localhost:9002/eureka
instance:
instance-id: resumer-user8002 #èªå®ä¹æå¡å称信æ¯
prefer-ip-address: true #访é®è·¯å¾å¯ä»¥æ¾ç¤ºIPå°
info:
app.name: resumer-user
company.name: ASIAINFO
build.artifactId: $project.artifactId$
build.version: $project.version$
注æï¼resumer-user #å¾®æå¡çååï¼å°è¢«spring-cloud-feignè°ç¨å¾®æå¡ï¼æå¡æ¶è´¹ç«¯ï¼ãresumer-feignãè°ç¨ã
å¯å¨ç±»ProviderApplication.java
package com.asiainfo.resumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
æ§å¶å±controller-UserController.javaç±»
package com.asiainfo.resumer.controller;
import java.util.List;
import com.asiainfo.resumer.User;
import com.asiainfo.resumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService service;
/**
* æµè¯æ¹æ³
* <p>Title: getUserTest</p>
* <p>Description: </p>
* @param id
* @return
*/
@CrossOrigin(origins = "*")// æ¯æè·¨å
@RequestMapping(value="/gettest/{id}", method=RequestMethod.GET)
public User getUserTest(@PathVariable("id") int id){
User user = service.getUser(id);
return user;
}
}
ä¸å¡å±Service-ï¼å æ¬æ¥å£åå®ç°ï¼ åæ°æ®å±dao
package com.asiainfo.resumer.service;
import java.util.List;
import com.asiainfo.resumer.User;
public interface UserService {
public User getUser(int id);
}
package com.asiainfo.resumer.service.impl;
import java.util.List;
import com.asiainfo.resumer.User;
import com.asiainfo.resumer.dao.UserDao;
import com.asiainfo.resumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUser(int id) {
User user = userDao.getUser(id);
System.out.println("microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦");
System.out.println("user : " + user);
return user;
}
}
package com.asiainfo.resumer.dao;
import java.util.List;
import com.asiainfo.resumer.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao {
public User getUser(int id);
}
æ°æ®å±mybatiså ³èä¿¡æ¯
UserMapper.xmlãresources/mybatis/mapper/UserMapper.xmlã
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.asiainfo.resumer.dao.UserDao">
<select id="getUser" resultType="User" parameterType="int">
select * from user where ID=#{id}
</select>
</mapper>
mybatis.cfg.xmlãresources/mybatis/mybatis.cfg.xmlã
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- å¼å¯äºçº§ç¼å -->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
æ°æ®åºuse表èæ¬ï¼
CREATE TABLE `user` (
`ID` int(10) NOT NULL,
`NAME` varchar(50) DEFAULT NULL,
`AGE` varchar(50) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into `user` (`ID`, `NAME`, `AGE`) values('1','CJ','30');
2.2 resumer-user2forTest ymlé ç½®
server:
port: 8003
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatisé
ç½®æ件æå¨è·¯å¾
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapperæ å°æ件
type-aliases-package: com.asiainfo.resumer # å«åç±»æå¨å
spring:
application:
name: resumer-user #å¾®æå¡çåå
datasource:
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱å¨å
type: com.alibaba.druid.pool.DruidDataSource # å½åæ°æ®æºæä½ç±»å
url: jdbc:mysql://localhost:3306/cloudagenttest # æ°æ®åºå称
username: root
password: root
dbcp2:
initial-size: 5 # åå§åè¿æ¥æ°
max-total: 5 # æ大è¿æ¥æ°
max-wait-millis: 200 # çå¾
è¿æ¥è·åçæ大è¶
æ¶æ¶é´
min-idle: 5 # æ°æ®åºè¿æ¥æ± çæå°ç»´æè¿æ¥æ°
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka,http://localhost:9002/eureka
instance:
instance-id: resumer-user8003 #èªå®ä¹æå¡å称信æ¯
prefer-ip-address: true #访é®è·¯å¾å¯ä»¥æ¾ç¤ºIPå°
info:
app.name: resumer-user
company.name: ASIAINFO
build.artifactId: $project.artifactId$
build.version: $project.version$
注æï¼resumer-user #å¾®æå¡çåå å resumer-userèç¹å称ä¿æä¸è´ã
3 spring-cloud-feignè°ç¨å¾®æå¡ï¼æå¡æ¶è´¹ç«¯ï¼ï¼ãresumer-feignã
3.1 é ç½®æ件yml
server:
port: 7001
eureka:
client:
register-with-eureka: false#ä¸å¨Eurekaä¸æ³¨åä¸æ¾ç¤º
service-url:
defaultZone: http://localhost:9001/eureka,http://localhost:9002/eureka
3.2 å¯å¨ç±»-FeignConsumerApplication
package com.asiainfo.resumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages="com.asiainfo.resumer.service")
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
RestContolleræ§å¶å±-UserConsumerController
package com.asiainfo.resumer.controller;
import java.util.List;
import com.asiainfo.resumer.User;
import com.asiainfo.resumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserConsumerController {
/**
* test method
* <p>Title: getTest</p>
* <p>Description: </p>
* @param id
* @return
*/
@RequestMapping(value="/consumer/gettest/{id}")
public User getTest(@PathVariable("id") int id){
User user = service.gettest(id);
return user;
}
}
Feginæ¶è´¹æå¡æ¥å£-ConsumerService
package com.asiainfo.resumer.service;
import java.util.List;
import com.asiainfo.resumer.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/*以åè°ç¨resumer-userå¾®æå¡ä¸çæ¹æ³ï¼åªéè¦è°ç¨ä¸é¢å¯¹åºçæ¥å£æ¢å¯ä»¥äº*/
@FeignClient(value="resumer-user")
public interface ConsumerService {
/*test*/
@RequestMapping(value="/gettest/{id}", method=RequestMethod.GET)
public User gettest(@PathVariable("id") int id);
}
4 è¿è¡ææ
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2csEDbHVWMVpmYxAnMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLygTM4IjNwMTMxAjNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
åå°éè¿è®¿é®Feginå°å http://192.168.0.104:7001/consumer/gettest/1ï¼ å·æ°10次ï¼
resumer-feign èç¹ consoleæ§å¶å°ï¼
2021-06-01 13:11:57.467 INFO 12876 â [nio-7001-exec-1] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client resumer-user initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=resumer-user,current list of Servers=[LAPTOP-R78VLQ0F:8003, LAPTOP-R78VLQ0F:8002],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:2; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:LAPTOP-R78VLQ0F:8002; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
, [Server:LAPTOP-R78VLQ0F:8003; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework[email protected]
å¾®æå¡ï¼æä¾æå¡ï¼èç¹ä¸¤ä¸ªãresumer-userãresumer-user2forTestã consoleæ§å¶å°ï¼åå«ååºç次æ°è®°å½ï¼
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
enter user2 for test webâ¦
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
enter user2 for test webâ¦
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
enter user2 for test webâ¦
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]
enter user2 for test webâ¦
microservice-providerå¾®æå¡å¨ååºå®¢æ·ç«¯è¯·æ±â¦â¦
user : User [id=1, name=CJ, age=30]