java.net.ConnectException:拒绝连接:使用Spring启动连接Solr

时间:2017-01-20 11:39:35

标签: java spring spring-mvc spring-boot solr

我是solr的新手。我尝试使用solr spring数据工具使用rest调用从表中获取记录。但是当我尝试从浏览器请求url时,我收到以下错误。

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_25]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.8.0_25]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_25]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_25]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_25]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_25]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_25]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_25]

我的代码如下:

用户对象:

public class User {

    @Field
    private String id;

    @Field
    private String fName;

    @Field
    private String lName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }
}

userDAO的:

public interface UserDao extends SolrCrudRepository<User, Long> {

}

UserController中:

@RestController
@RequestMapping("/app/api")
public class UserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public AjaxResponse loadAll() throws IOException {

        List<User> users = (List<User>) userDao.findAll();
        return new AjaxResponse("Success", false, users);
    }
}

Aplication.properties文件:

server.port=9111

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/solrDatabase?autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root

spring.data.solr.host=http://localhost:8983/solr/
spring.data.solr.repositories.enabled=true

当我尝试请求此网址时: http://localhost:9111/app/api/users 然后我得到例外。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:2)

抛出java.net.ConnectException: Connection refused: connect异常,因为您的Spring应用程序无法连接到Solr服务器。由于您提供的网址http://localhost:8983/solr/在浏览器中打开时无效,因此Solr可能无法正常运行(只要您尝试从与代码运行的主机相同,其他示例URL似乎是真的。)

确保Solr实际上正在运行,并且能够在对您的应用程序进行任何更改之前回答请求。

答案 1 :(得分:1)

将以下属性添加到application.properties中:

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false